#include "GY_GraphicsLineItem.h"
#include <QPainter>
#include <QDebug>

GY_GraphicsLineItem::GY_GraphicsLineItem(const QString &guid,QGraphicsItem *parent)
    :GY_BasicsGraphicsObject(guid,this,parent)
{
    //图元类型
    GY_Object::setUserData(GY_ENUM::ClassType,QVariant("Line"));
    initializationPropertyInfo();
    updataPen();
    updataBrush();
    QPointF point1=m_PropertyInfoMap.value("Point1").propertyValue.value<QPointF>();
    QPointF point2=m_PropertyInfoMap.value("Point2").propertyValue.value<QPointF>();
    updatePath(point1, point2);
    QMap<QVariant,QPointF> editPoints = {{"Point1",point1},{"Point2",point2}};
    this->setPen(QColor(Qt::green));
    this->setEditPointF(editPoints);
}

GY_GraphicsLineItem::GY_GraphicsLineItem(const QString &guid,qreal x1, qreal y1, qreal x2, qreal y2, QGraphicsItem *parent) :GY_BasicsGraphicsObject(guid,this,parent)
{
    //图元类型
    GY_Object::setUserData(GY_ENUM::ClassType,QVariant("Line"));
    initializationPropertyInfo();
    updataPen();
    updataBrush();
    QPointF point1(x1,y1);
    QPointF point2(x2,y2);
    m_PropertyInfoMap["Point1"].propertyValue=point1;
    m_PropertyInfoMap["Point2"].propertyValue=point2;
    updatePath(point1, point2);
    QMap<QVariant,QPointF> editPoints = {{"Point1",point1},{"Point2",point2}};
    this->setEditPointF(editPoints);
}

void GY_GraphicsLineItem::initializationPropertyInfo()
{
    /*
     *LineStyle属性的数据
     * QVector<qreal>中index(0)是Qt::PenCapStyle、Qt::PenJoinStyle与Qt::PenStyle的组合,index(1)为线条粗细，之后是自定义钢笔样式的数据 */
    QVector<qreal> varStyle;
    int style = Qt::SquareCap|Qt::BevelJoin|Qt::CustomDashLine;
    varStyle<<style<<2<<3<<2<<4<<5<<6<<7;

    GY_PropertyInfo point1PropertyInfo(this);
    point1PropertyInfo.propertyName="Point1";
    point1PropertyInfo.propertyValue=QPointF(0,0);
    point1PropertyInfo.updateType = MoveUpdate|Update|PointOfRelative;
    point1PropertyInfo.propertyType = PointPropertyType;
    point1PropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicsLineItem::updatePoint;
    m_PropertyInfoMap.insert("Point1",point1PropertyInfo);
    m_PropertyInfoMap[point1PropertyInfo.propertyName].setConnect();

    GY_PropertyInfo point2PropertyInfo(this);
    point2PropertyInfo.propertyName="Point2";
    point2PropertyInfo.propertyValue=QPointF(50,50);
    point2PropertyInfo.updateType = MoveUpdate|Update|PointOfRelative;
    point2PropertyInfo.propertyType = PointPropertyType;
    point2PropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicsLineItem::updatePoint;
    m_PropertyInfoMap.insert("Point2",point2PropertyInfo);
    m_PropertyInfoMap[point2PropertyInfo.propertyName].setConnect();

    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update|OrdinaryAttributes;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicsLineItem::updateObjectName;
    m_PropertyInfoMap.insert("ObjectName",ObjectName);
    m_PropertyInfoMap[ObjectName.propertyName].setConnect();

    GY_PropertyInfo IsAntialiased(this);
    IsAntialiased.propertyName = "IsAntialiased";
    IsAntialiased.propertyValue = false;
    IsAntialiased.updateType = Update|OrdinaryAttributes;
    IsAntialiased.propertyType = BoolPropertyType;
    IsAntialiased.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicsLineItem::updateIsAntialiased;
    m_PropertyInfoMap.insert("IsAntialiased",IsAntialiased);
    m_PropertyInfoMap[IsAntialiased.propertyName].setConnect();

    GY_PropertyInfo PenAttributes(this);
    PenAttributes.propertyName = "PenAttributes";
    PenAttributes.propertyValue = false;
    PenAttributes.updateType = Update|OrdinaryAttributes;
    PenAttributes.propertyType = BoolPropertyType;
    PenAttributes.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicsLineItem::updatePenAttributes;
    m_PropertyInfoMap.insert("PenAttributes",PenAttributes);
    m_PropertyInfoMap[PenAttributes.propertyName].setConnect();

    GY_PropertyInfo PenColor(this);
    PenColor.propertyName = "PenColor";
    PenColor.propertyValue = QColor();
    PenColor.updateType = Update|OrdinaryAttributes;
    PenColor.propertyType = ColorPropertyType;
    PenColor.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicsLineItem::updatePenColor;
    m_PropertyInfoMap.insert("PenColor",PenColor);
    m_PropertyInfoMap[PenColor.propertyName].setConnect();

    GY_PropertyInfo LineStyle(this);
    LineStyle.propertyName = "LineStyle";
//    LineStyle.propertyValue = QVariant::fromValue(varStyle);
    LineStyle.propertyValue = QVariant();
    LineStyle.updateType = Update|OrdinaryAttributes;
    LineStyle.propertyType = LineStylePropertyType;
    LineStyle.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicsLineItem::updateLineStyle;
    m_PropertyInfoMap.insert("LineStyle",LineStyle);
    m_PropertyInfoMap[LineStyle.propertyName].setConnect();

    GY_PropertyInfo PenOpacity(this);
    PenOpacity.propertyName = "PenOpacity";
    PenOpacity.propertyValue = 1;
    PenOpacity.updateType = Update|OrdinaryAttributes;
    PenOpacity.propertyType = QrealPropertyType;
    PenOpacity.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicsLineItem::updatePenOpacity;
    m_PropertyInfoMap.insert("PenOpacity",PenOpacity);
    m_PropertyInfoMap[PenOpacity.propertyName].setConnect();

    GY_PropertyInfo IsVisible(this);
    IsVisible.propertyName = "IsVisible";
    IsVisible.propertyValue = true;
    IsVisible.updateType = Update|OrdinaryAttributes;
    IsVisible.propertyType = BoolPropertyType;
    IsVisible.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_GraphicsLineItem::updateIsVisible;
    m_PropertyInfoMap.insert("IsVisible",IsVisible);
    m_PropertyInfoMap[IsVisible.propertyName].setConnect();

}

void GY_GraphicsLineItem::updatePath(const QPointF &p1, const QPointF &p2)
{
    QPainterPath path = this->path();
    path.clear();
    path.moveTo(p1);
    path.lineTo(p2);
    this->setPath(path);
}
GY_GraphicsLineItem::~GY_GraphicsLineItem()
{

}

bool GY_GraphicsLineItem::updatePoint(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QPointF>())
    {
        return false;
    }
    QPointF  pointf=value.value<QPointF>();
    //坐标系转换（重要）
    QPointF pf = this->mapFromScene(pointf);
    QMap<QVariant,QPointF> map = this->getEditPointF();
    //根据编辑点名称对应修改的点
    if(name.endsWith("Point1")){
        updatePath(this->getEditPointF().value("Point2"), pf);
        m_PropertyInfoMap["Point1"].propertyValue=pf;
        map.insert("Point1",pf);
    }else if(name.endsWith("Point2")){
        updatePath(this->getEditPointF().value("Point1"), pf);
        m_PropertyInfoMap["Point2"].propertyValue=pf;
        map.insert("Point2",pf);
    }
    this->setEditPointF(map);
    return true;
}

